<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: 牧羊人 <1175401194@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\OfPatentMonitor;
use app\common\service\BaseService;
use think\facade\Db;

/**
 * 专利监控管理-服务类
 * @author 牧羊人
 * @since: 2022/01/07
 * Class OfPatentMonitorService
 * @package app\admin\service
 */
class OfPatentMonitorService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new OfPatentMonitor();
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since 2022/01/07
     * @author 牧羊人
     */
    public function getList()
    {
        $param = request()->param();

        // 查询条件
        $map = [];
		$map[] = ['status','<>',-1];

        $list = parent::getList($map);
        
        return $list; // TODO: Change the autogenerated stub
    }


                                            
    /**
     * 生成监控数据
     */
    public function monitor($id=0)
    {   
        $wh = [];
        $wh[] = ['id','=',$id];
        $wh[] = ['status','=',6];
        $wh[] = ['issue_date','<>',''];
        $wh[] = ['apply_date','<>',''];
        $wh[] = ['number','<>',''];
        $info = Db::name("of_patent")->where($wh)->find();
        if(!empty($info)){
            $wh = [];
            $wh[] = ['patent_id','=',$id];
            // $wh[] = ['mark','=',1];
            $pinfo = Db::name("of_patent_monitor")->where($wh)->find();
            if(empty($pinfo)){
                $company_name = Db::name("of_company")->where("id","=",$info['company_id'])->value("name");
                $apply_time = strtotime($info['apply_date']);
                $compare = strtotime("+5 month",strtotime($info['issue_date']));

                for ($num=1; $num < 50; $num++) { 
                    $apply = strtotime("+$num year",$apply_time);
                    if($apply > $compare){
                        break;
                    }
                }
                $pay_time = strtotime("+2 month",strtotime($info['issue_date']));
                $pay_time = strtotime("+15 day",strtotime($info['issue_date']));
                $data = [];
                $data['patent_id']      = $id;
                $data['number']         = $info['number'];
                $data['apply_time']     = $apply_time;
                $data['pay_time']       = $pay_time;
                $data['company_name']   = $company_name;
                $data['patent_name']    = $info['patent_name'];
                $data['year']           = $num;     
                Db::name("of_patent_monitor")->insert($data);
            }
        }
        return true;
    }
                                  
}